package com.itheima.controller.backend;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.pojo.vo.OrderSettingVO;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {
    @Autowired
    private OrderSettingService orderSettingService;

    /**
     * 获取预约设置数据
     *
     * @param month
     * @return
     */
    @GetMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String month) {
        List<OrderSettingVO> reslut = orderSettingService.getOrderSettingByMonth(month);
        if (reslut != null) {
            return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS, reslut);
        } else {
            return new Result(false, MessageConstant.QUERY_ORDER_FAIL);
        }

    }


    /**
     * 根据日期修改预约设置
     *
     * @param orderSetting
     * @return
     */
    @PostMapping("/editNumberByOrderDate")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting) {
        Boolean success = orderSettingService.editNumberByDate(orderSetting);
        if (success) {
            return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
        } else {
            return new Result(false, MessageConstant.ORDERSETTING_FAIL);
        }
    }


    /**
     * 下载预约设置模板
     * @param uri
     * @return
     */
    @GetMapping("/download")
    public String downloadTemplate(@RequestParam("filename") String uri, HttpServletResponse response) {
        orderSettingService.download(uri, response);
        return "文件下载";
    }

    /**
     * 上传预约设置模板
     * @param excelFile
     * @return
     */
    @RequestMapping("/upload")
    public Result upload(MultipartFile excelFile) {
        orderSettingService.upload(excelFile);
        return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
    }
}
